home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / MENUSYST.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  119 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5. /* Modified for Quintus Prolog by Andreas Siebert */
  6.  
  7. /* MENUSYST.PL */
  8. /* A menu system written in Prolog */
  9.  
  10. /* Requires procedure MENU on file MENU.PL */
  11. :- ( clause(menu(_,_),_) ; consult('menu.pl') ).
  12.  
  13. /****************************
  14.  * Menu navigation routines *
  15.  ****************************/
  16.  
  17. /* back(Path)
  18.  *   Go to the previous menu. If none, write an
  19.  *   error message and return to the current menu.
  20.  */
  21.  
  22. back([Head|Tail]) :-  /* back to previous menu */
  23.   !,
  24.   Goal =.. [Head,Tail],    /* note comma! */
  25.   call(Goal).
  26.  
  27. back([]) :-           /* there IS no previous menu */
  28.   Goal =.. [_,[]],
  29.   write('Cannot back up further'),write(Goal),nl,
  30.   call(Goal).
  31.  
  32. /* forward(NewMenu,CurrentMenu,Path)
  33.  *   Go to NewMenu, passing it a correctly constructed path.
  34.  */
  35.  
  36. forward(NewMenu,CurrentMenu,Path) :-   /* forward to another menu */
  37.   Goal =.. [NewMenu,[CurrentMenu|Path]],
  38.   call(Goal).
  39.  
  40. /* application(Name,CurrentMenu,Path)
  41.  *   Execute the specified application, then
  42.  *   return to the current menu.
  43.  */
  44.  
  45. application(Name,CurrentMenu,Path) :-  /* execute an application */
  46.   call(Name),
  47.   Goal =.. [CurrentMenu,Path],
  48.   call(Goal).
  49.  
  50. /*********************************
  51.  * Menus (replace with your own) *
  52.  *********************************/
  53.  
  54. /* Each menu takes an argument giving the list of menus
  55.    through which it was reached (most recent first).
  56.    See text for comments. */
  57.  
  58. menuA(_) :-
  59.   nl,
  60.   write('Main menu (Menu A)'),nl,
  61.   menu([item('Go to menu B', forward(menuB,menuA,[])),
  62.         item('Go to menu C', forward(menuC,menuA,[])),
  63.         item('Exit to operating system',
  64.           application(exit_to_operating_system,_,_)),
  65.         item('Exit to Prolog',application(exit_to_prolog,_,_))],
  66.        Goal),
  67.   call(Goal).
  68.  
  69. menuB(Path) :-
  70.   nl,
  71.   write('Menu B'),nl,
  72.   menu([item('Go to menu D', forward(menuD,menuB,Path)),
  73.         item('Go to menu E', forward(menuE,menuB,Path)),
  74.         item('Previous menu',back(Path))],
  75.        Goal),
  76.   call(Goal).
  77.  
  78. menuC(Path)  :-
  79.   nl,
  80.   write('Menu C'),nl,
  81.   menu([item('Say hello',    application(say_hello,menuC,Path)),
  82.         item('Say bonjour',  application(say_bonjour,menuC,Path)),
  83.         item('Previous menu',back(Path))],
  84.        Goal),
  85.   call(Goal).
  86.  
  87. menuD(Path) :-
  88.   nl,
  89.   write('Menu D'),nl,
  90.   menu([item('Exit to Prolog',application(exit_to_prolog,_,_)),
  91.         item('Jump to menu C',forward(menuC,menuD,Path)),
  92.         item('Previous menu', back(Path))],
  93.        Goal),
  94.   call(Goal).
  95.  
  96. menuE(Path) :-
  97.   nl,
  98.   write('Menu E'),nl,
  99.   menu([item('Exit to operating system', 
  100.            application(exit_to_operating_system,menuE,Path)),
  101.         item('Say hello',        application(say_hello,menuE,Path)),
  102.         item('Previous menu',    back(Path)),
  103.         item('Jump to main menu',forward(menuA,menuE,Path))],
  104.        Goal),
  105.   call(Goal).
  106.  
  107. /* The applications. These are trivial examples,
  108.    but any Prolog procedures could be substituted. */
  109.  
  110. say_hello :- write('Hello'),nl.
  111. say_bonjour :- write('Bonjour'),nl.
  112. exit_to_operating_system :- halt.
  113. exit_to_prolog :- abort.
  114.  
  115. /* Starting query */
  116. start :- menuA(_).
  117.  
  118. :-start.
  119.